import 'package:flutter/material.dart';
import '../../../scoped_model/auth.dart';
import '../../../utils/scoped_model_helper.dart';

var textStyle = TextStyle(
  fontSize: 20,
  fontWeight: FontWeight.w600,
  color: Colors.white,
);

class Header extends StatelessWidget {
  const Header({Key key}) : super(key: key);

  Widget _notLoginBuilder(BuildContext context) {
    return Container(
      height: 110.0,
      padding: EdgeInsets.all(16.0),
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          Container(
            width: 100.0,
            height: 100.0,
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                  'https://tva1.sinaimg.cn/large/008i3skNgy1gsuh24kjbnj30do0duaad.jpg'),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(top: 10.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pushNamed('login');
                      },
                      child: Text(
                        '登录',
                        style: textStyle,
                      ),
                    ),
                    Text(
                      '/',
                      style: textStyle,
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pushNamed('register');
                      },
                      child: Text(
                        '注册',
                        style: textStyle,
                      ),
                    )
                  ],
                ),
              ),
              Text(
                '登录后可以体验更多',
                style: TextStyle(color: Colors.white),
              )
            ],
          )
        ],
      ),
    );
  }

  Widget _loginBuilder(BuildContext context) {
    var userInfo = ScopedModelHelper.getModel<AuthModel>(context).userInfo;
    String userNickname = userInfo?.nickname ?? '已登录用户';
    String userImage = userInfo?.avatar ??
        'https://tva1.sinaimg.cn/large/008i3skNgy1gsuhtensa6j30lk0li76f.jpg';
    return Container(
      height: 110.0,
      padding: EdgeInsets.all(16.0),
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          Container(
            width: 100.0,
            height: 100.0,
            child: CircleAvatar(
              backgroundImage: NetworkImage(userImage),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(top: 10.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text(
                      userNickname,
                      style: textStyle,
                    )
                  ],
                ),
              ),
              Text(
                '查看编辑个人资料',
                style: TextStyle(color: Colors.white),
              )
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var isLogin = ScopedModelHelper.getModel<AuthModel>(context).isLogin;
    return isLogin ? _loginBuilder(context) : _notLoginBuilder(context);
  }
}
